package net.myspring.middle.web;

import net.myspring.common.domain.SearchEntity;
import net.myspring.common.utils.Const;
import net.myspring.common.utils.Message;
import net.myspring.common.utils.RequestUtils;
import net.myspring.middle.domain.Menu;
import net.myspring.middle.service.MenuService;
import net.myspring.middle.validator.MenuValidator;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * Created by lenvov on 2016/8/14.
 */
@Controller
@RequestMapping(value = "sys/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;
    @Autowired
    private MenuValidator menuValidator;

    @RequestMapping(value = "list")
    @PreAuthorize("hasAnyAuthority('PERM_sys:menu:list:view')")
    public String list(Model model, HttpServletRequest request) {
        SearchEntity searchEntity = RequestUtils.getSearchEntity(request, Menu.class.getName());
        model.addAttribute("page", menuService.findPage(searchEntity.getPageable(), searchEntity.getParams()));
        model.addAttribute("s", searchEntity);
        return "sys/menuList";
    }

    @RequestMapping(value = "form")
    @PreAuthorize("hasAnyAuthority('PERM_sys:menu:list:edit')")
    public String form(Model model, Menu menu) {
        model.addAttribute("menu", menu.getId() == null ? menu : menuService.findOne(menu.getId()));
        addAttributes(model);
        return "sys/menuForm";
    }

    @RequestMapping(value = "save")
    @PreAuthorize("hasAnyAuthority('PERM_sys:menu:list:edit')")
    public String save(Model model, Menu menu, BindingResult bindingResult, RedirectAttributes redirectAttributes) {
        boolean isCreate = menu.isCreate();
        menuValidator.validate(menu, bindingResult);
        if (bindingResult.hasErrors()) {
            model.addAttribute("bindingResult", bindingResult);
            addAttributes(model);
            return "sys/menuForm";
        }
        menuService.save(menu);
        redirectAttributes.addFlashAttribute("message", new Message("菜单保存成功"));
        if (isCreate) {
            return "redirect:/sys/menu/form";
        } else {
            return "redirect:/sys/menu/list";
        }
    }

    @RequestMapping(value = "delete")
    @PreAuthorize("hasAnyAuthority('PERM_sys:menu:list:delete')")
    public String delete(Model model, Menu menu, RedirectAttributes redirectAttributes) {
        menuService.delete(menu);
        return "redirect:/sys/menu/list";
    }

    @RequestMapping(value = "checkName")
    @ResponseBody
    public boolean checkName(Menu menu) {
        return menuService.checkName(menu);
    }

    @RequestMapping(value = "checkLink")
    @ResponseBody
    public boolean checkLink(Menu menu) {
        return menuService.checkLink(menu);
    }

    private void addAttributes(Model model) {
        model.addAttribute("bools", Const.BoolEnum.getMap());
        model.addAttribute("menuList", menuService.findAll());
        model.addAttribute("menuTypeMap", Menu.MenuType.getMap());
    }
}
